#!/bin/sh
#/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#/* Copyright 2002-2003 by Mark Wolgemuth, All rights reserved.             */
#/* Copyright 2002-2003 by Employease, Inc.  All rights reserved.           */
#/*                                                                         */
#/* Redistribution and use in source and binary forms, with or without      */
#/* modification, are permitted provided that the following conditions      */
#/* are met:                                                                */
#/*                                                                         */
#/*    1. Redistributions of source code must retain the above copyright    */
#/* notice, this list of conditions and the following disclaimer.           */
#/*    2. Redistributions in binary form must reproduce the above copyright */
#/* notice, this list of conditions and the following disclaimer in the     */
#/* documentation and/or other materials provided with the distribution.    */
#/*                                                                         */
#/* THIS SOFTWARE IS PROVIDED BY THE MOD_ATHENA PROJECT ``AS IS'' AND ANY   */
#/* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE       */
#/* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR      */
#/* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT OR        */
#/* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,   */
#/* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,     */
#/* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR      */
#/* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY     */
#/* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT            */
#/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE   */
#/* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.    */
#/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

HTTP_TIMEOUT=10
# set this if you want to override search
AGENT_BIN=""
# don't change contents, change order is ok
AGENT_CHOICES="fetch curl wget lynx"
AGENT_OPTS_fetch="-o - -q -T $HTTP_TIMEOUT"
AGENT_OPTS_curl="-s --connect-timeout $HTTP_TIMEOUT"
AGENT_OPTS_wget="-O - -q --timeout=$HTTP_TIMEOUT"
AGENT_OPTS_lynx="-dump -connection-timeout=$HTTP_TIMEOUT"
ATH_SENDMAIL_BIN="/usr/sbin/sendmail -t"
PERL=/usr/bin/perl

setup() {
    umask 022
}

unixtime() {
    ${PERL} -e "print STDOUT time();"
}

# status_msg() echo wrapper to handle quiet mode
status_msg() {
    if [ "${ATH_OPT_QUIET}" != "1" ]
    then
	if [ "${ATH_OPT_LOG}" = "1" ]
	then
	    if [ "${ATH_OPT_LOGFILE}" != "" ]
	    then
		echo $@ >> ${ATH_OPT_LOGFILE}
	    else
		logger -i -t "ath_agent.sh" -p local0.notice "$@"
	    fi
	else
	    echo $@
	fi
    fi
    return 0
}

# chk_agent_gin() sets program to use for user agent
chk_agent_bin() {
    if [ "${PATH}" != "" ]
    then
	AGENT_PATH_DIRS=`echo ${PATH} | sed -e 's/:/ /g'`
    else
	AGENT_PATH_DIRS="/bin /usr/bin /usr/local/bin /opt/bin /opt/local/bin /sw/bin ${HOME}/bin ./"
    fi

    for AGENT_TEST_BIN in ${AGENT_CHOICES}; do
	for AGENT_BASE_DIR in ${AGENT_PATH_DIRS}; do
	    if ( test -x "${AGENT_BASE_DIR}/${AGENT_TEST_BIN}" )
	    then
		AGENT_DIR=${AGENT_BASE_DIR}
		AGENT_BIN=${AGENT_TEST_BIN}
		break
	    fi
	done
	if [ "${AGENT_BIN}" != "" ]
	then
	    break
	fi
    done

    if [ "${AGENT_BIN}" = "" ]
    then
	status_msg "No http/https user agent found in your PATH."
	exit
    fi
    return 0
}

# add_agent_opts() adjust GET_CMD for each agent program
add_agent_opts() {
    case ${AGENT_BIN} in
    "fetch")
	AGENT_OPTS="${AGENT_OPTS_fetch}"
	;;
    "wget")
	AGENT_OPTS="${AGENT_OPTS_wget}"
	;;
    "lynx")
	AGENT_OPTS="${AGENT_OPTS_lynx}"
	;;
    "curl")
	AGENT_OPTS="${AGENT_OPTS_curl}"
	;;
    *)
	status_msg "WARNING: ${AGENT_BIN} has no OPTS setting"
    esac
    return 0
}

# update_loop() loops the update
loop() {
    while true 
    do
	loop_run
	sleep "${ATH_OPT_FREQ}"
    done
    return 0
}

loop_bg() {
    status_msg "Entering nohup background mode; pid in $ATH_PID_FILE"
    trap '' 1
    trap 'cleanup_bg_exit' 2 3 15 17 18
    loop
}

loop_run() {
    echo "You must override this."
    exit 1
}

loop_kill() {
    if test -e $ATH_PID_FILE
    then
	MYPID=`cat $ATH_PID_FILE`
	status_msg "Sending kill to $MYPID"
	KILL_CMD=`kill $MYPID`
	if [ "x$KILL_CMD" != "x" ]
	then
	    status_msg "Result: $KILL_CMD"
	fi
    else
	status_msg "No pid file found"
    fi
}

cleanup_bg_exit() {
    rm $ATH_PID_FILE
    status_msg "Exiting on user signal"
    exit 0
}

exec_handler() {
    status_msg "Background: ${ATH_OPT_BG} Base URL: ${ATH_OPT_BURL}"
    status_msg "Agent: ${AGENT_BIN} Frequency: ${ATH_OPT_FREQ}"
    if [ "${ATH_OPT_BG}" = "YES" ]
    then
	loop_bg &
	echo "$!" > $ATH_PID_FILE
    else
	loop
    fi
}

chk_ath_return() {
    if [ "$1" = "ERROR:" ]
    then
	echo "FAIL"
    else
	echo "PASS"
    fi
}

